#!/usr/bin/env python
"""
# -*- coding: utf-8 -*-
# @Time    : 2020/9/24 23:08
# @Author  : AshJo
# @Desc    : 反射
# @Filename: reflection.py
# @Version : v1.0
"""
import sys


class Payment:
    def pay(self, money):
        raise NotImplementedError('请在子类中重写同名pay方法')


class Alipay(Payment):
    def __init__(self, name):
        self.name = name

    def pay(self, money):
        dic = {'username': self.name, 'price': money}
        # 想办法调用支付宝的支付接口 url连接 把dic传过去
        print('%s用户通过支付宝支付了%s元' % (self.name, money))


class WeChat(Payment):
    def __init__(self, name):
        self.name = name

    def pay(self, money):
        dic = {'username': self.name, 'price': money}
        # 想办法调用微信支付接口 url连接 把dic传过去
        print('%s用户通过微信支付了%s元' % (self.name, money))


class QQPay(Payment):
    def __init__(self, name):
        self.name = name

    def pay(self, money):
        dic = {'username': self.name, 'price': money}
        # 想办法调用微信支付接口 url连接 把dic传过去
        print('%s用户通过QQ支付了%s元' % (self.name, money))


# 利用反射的特性简化程序，去掉对传入的kind实参进行的if..elif判断过程
def pay(name, money, kind):
    class_name = getattr(sys.modules['__main__'], kind)
    obj = class_name(name)
    obj.pay(money)


pay('alex', 888, 'Alipay')
pay('alex', 998, 'WeChat')
pay('alex', 566, 'QQPay')
